package com.wswy.wzcx.ui.fkdj

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.text.TextUtils
import com.che.libcommon.ui.BaseFragment
import com.wswy.wzcx.R
import com.wswy.wzcx.api.ConfigsKt
import com.wswy.wzcx.model.DataCenter
import com.wswy.wzcx.module.InternalMsgCodeKt
import com.wswy.wzcx.module.base.CBaseActivity
import com.wswy.wzcx.ui.other.CloseDelegate
import com.wswy.wzcx.ui.web.WebViewActivity
import kotlinx.android.synthetic.main.activity_fkdj_payment.*

/**
 * 罚款代缴
 */
class FinePaymentActivity : CBaseActivity() {

    private lateinit var fineViewModel: FineViewModel

    private lateinit  var closeDelegate: CloseDelegate

    private var fragment:BaseFragment?=null

    private var preFragment:BaseFragment?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fkdj_payment)
        setAppTitle(getString(R.string.module_fkdj))
        closeDelegate = CloseDelegate(this,InternalMsgCodeKt.SUBMIT_COMPLETE_FKDJ)
        supportFragmentManager.beginTransaction().replace(R.id.fl_content,FineOrderFragment().apply {
            arguments = intent?.extras
            preFragment = this
        }).commitNow()

        fineViewModel = provideViewModel(FineViewModel::class.java)
        fineViewModel.onCreate()

        fineViewModel.getCheckFineLiveData().observe(this, Observer {
            it?.run {
                if(isOk()){
                    supportFragmentManager.beginTransaction().run {
                        preFragment?.let {
                            hide(it)
                        }
                        fragment = FinePaymentFragment()

                        fragment?.let {

                            if (!it.isAdded){
                                add(R.id.fl_content,it)
                            }

                            show(it)
                        }

                        commitNow()

                    }

                    tv_submit.setText(R.string.issues)
                }

            }

        })

        img_back.setOnClickListener {
            if (!handleBack()){
                finish()
            }
        }


        tv_submit.setOnClickListener {
            if (TextUtils.equals(getString(R.string.my_order),tv_submit.text)){
                //我的订单
                if (DataCenter.get().hasLogin()){
                    WebViewActivity.startWebview(it.context,ConfigsKt.H5_ORDER_LIST().format(0, DataCenter.get().userMode?.token))
                }else{
                    fineViewModel.gotoLogin(it.context)
                }
            }else if(TextUtils.equals(getString(R.string.issues),tv_submit.text)){
                //常见问题
                WebViewActivity.startWebview(it.context, ConfigsKt.H5_CJWT_FKDJ())
            }

        }

    }

    private fun handleBack():Boolean{
        if (fragment is FinePaymentFragment){

            supportFragmentManager.beginTransaction().run {

                fragment?.let {
                    remove(it)
                }

                fragment = null

                preFragment?.let {
                    show(it)
                }

                commitNow()

            }

            tv_submit.setText(R.string.my_order)

            return true
        }
        return false
    }

    override fun onResume() {
        super.onResume()
        fineViewModel.reloadCouponList()
    }

    override fun onBackPressed() {
//        if (fragment?.onBackPress() != true){
//            super.onBackPressed()
//        }

        if (!handleBack()){
            super.onBackPressed()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        closeDelegate.onDestroy()
    }
}